<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends Admin_Controller {

	function __construct(){
		parent::__construct();
		header('Content-type: text/html');
		header('Access-Control-Allow-Origin: *');
		
		$this->template->set_template('default');
	}
	
	/*-------------------------------------- FrontEnd --------------------------------------*/
	function index(){
		$data = array();
		$auction_list = $this->model->fetch('*','tb_auction','status = 1','end_time','desc',-1,0,true);
		$data['auction_list'] = $auction_list;
		$this->template->write_view('content','index',$data);
		$this->template->render();
	}
	
	function login(){
		$email = trim($this->input->post('email'));
		$password = $this->input->post('password');
		$redirect_url = PATH_URL;
		if($this->agent->is_referral()){
			$redirect_url = $this->agent->referrer();
		}
		$this->_do_login($email,$password,false,$redirect_url);
	}
	function _do_login($email,$password='',$is_open_id=false,$redirect_url=''){
		$redirect_url = !empty($redirect_url) ? $redirect_url : PATH_URL;
		$result = false;
		$user = $this->model->get('id,email,password,coin_paying,coin_number',ADMIN_USER_TABLE,"email = '{$email}'",true);
		if(!empty($user)){
			if($is_open_id){
				$result = true;
				$_SESSION['user'] = $user;
			} else {
				if($user['password'] == md5($password)){
					$_SESSION['user'] = $user;
				}
			}
		}
		if($is_open_id){
			return $result;
		} else {
			redirect($redirect_url);
		}
	}
	function login_open_id(){
		$type = $this->input->get('type');
		if($type == 'fb_sharp'){
			$data['action'] = 'reload';
			$this->load->view('login_open_id',$data);
		} else {
			$type = 'fb';
			$info = get_attr_profile_openid($type);
			$email = $info['email'];
			$result = $this->_do_login($email,'',true);
			if(!$result){
				$user['email'] = $email;
				$user['is_open_id'] = 1;
				if($this->model->insert(ADMIN_USER_TABLE,$user)){
					$this->_do_login($email,'',true);
				}
			}
			$data['action'] = 'close';
			$this->load->view('login_open_id',$data);
		}
	}
	function test_popup(){
		$this->load->view('test_popup');
	}
	
	function logout(){
		$redirect_url = PATH_URL;
		unset($_SESSION['user']);
		redirect($redirect_url);
	}
	
	function password_recovery(){
		$data = array();
		$this->template->write_view('content','password_recovery',$data);
		$this->template->render();
	}
	
	function register(){
		$data = array();
		$this->template->write_view('content','register',$data);
		$this->template->render();
	}
	function check_email(){
		$result = 'false';
		$email = trim($this->input->get('email'));
		if(!empty($email)){
			$user = $this->model->get('id',ADMIN_USER_TABLE,"email = '{$email}'",true);
			$result = empty($user) ? 'true' : 'false';
		}
		echo $result;
	}
	function register_submit(){
		$redirect_url = PATH_URL.'dang-ky';
		$email = trim($this->input->post('email'));
		$password = $this->input->post('password');
		if(!empty($email) && !empty($password)){
			$user = $this->model->get('id',ADMIN_USER_TABLE,"email = '{$email}'",true);
			if(empty($user)){
				$user['email'] = $email;
				$user['password'] = md5($password);
				if($this->model->insert(ADMIN_USER_TABLE,$user)){
					$this->_do_login($email,$password);
				}
			}
		}
		redirect($redirect_url);
	}
	
	function test_cron(){
		// echo exec('crontab u apache -l',$output, $arg);
		// pr($output);
		// echo $arg;
		// echo shell_exec('sudo crontab -l'); // Test sudo successfully
		echo shell_exec('sudo crontab /var/www/html/cron/crontab.txt');
		echo 'test_cron successfully!<br/>';
	}
	
	function paying(){
		$data = array();
		$this->template->write_view('content','paying',$data);
		$this->template->render();
	}
	function paying_cancel(){
		echo 'HỦY BỎ NẠP XU';
	}
	function paying_return(){
		if(!empty($_SESSION['pay_value']) && !empty($_SESSION['user'])){
			$pay_value = (int)$_SESSION['pay_value'];
			unset($_SESSION['pay_value']);
			$user = $_SESSION['user'];
			$user_id = $user['id'];
			$user_data = $this->model->get('id,email,password,coin_paying,coin_number',ADMIN_USER_TABLE,"id = {$user_id}",true);
			if(!empty($user_data)){
				$user_data['coin_paying'] += $pay_value;
				$coin_number_increment = floor($pay_value*10);
				$user_data['coin_number'] += $coin_number_increment;
				// Reupdate session
				$_SESSION['user'] = $user_data;
				$data['coin_number_increment'] = $coin_number_increment;
				if($this->model->update(ADMIN_USER_TABLE,$user_data,"id = {$user_id}")){
					$data['user'] = $user_data;
					$this->template->write_view('content','paying_return',$data);
					$this->template->render();
				}
			}
		}
	}
	function paying_submit(){
		if(!empty($_POST)){
			$pay_value = (int)$this->input->post('pay_value');
			$cancelUrl = PATH_URL."home/paying_cancel";
			$returnUrl = PATH_URL."home/paying_return";
			$receiver_email = PAYPAL_RECEIVER_EMAIL;
			$receiver_amount = $pay_value;
			$approve_url = paypal_get_paykey($cancelUrl,$returnUrl,$receiver_email,$receiver_amount);
			if($approve_url){
				$_SESSION['pay_value'] = $pay_value;
				$data['approve_url'] = $approve_url;
				$this->load->view('paying_submit',$data);
			}
		}
	}
	
	function winners(){
		$data['winner_list'] = $this->get_winner_list();
		$this->template->write_view('content','winners',$data);
		$this->template->render();
	}
	function get_winner_list(){
		$table_user = ADMIN_USER_TABLE;
		$query = "
				SELECT tb_auction.*, {$table_user}.email
				FROM tb_auction_bid INNER JOIN tb_auction ON tb_auction_bid.auction_id = tb_auction.id
				INNER JOIN {$table_user} ON tb_auction_bid.user_id = {$table_user}.id
				WHERE is_won = 1
				";
		$query = $this->db->query($query);
		$winner_list = $query->result_array();
		return $winner_list;
	}
	
	function instruction(){
		$data = array();
		$this->template->write_view('content','instruction',$data);
		$this->template->render();
	}
	
	function right_column(){
		$data['winner_list'] = $this->get_winner_list();
		$this->load->view('right_column',$data);
	}
}